package com.simon.annual.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import com.simon.annual.entity.ReportDebtfinancing;
import com.simon.annual.entity.ReportIndirectfinancing;
import com.simon.annual.entity.ReportNeeq;
import com.simon.annual.entity.ReportPrefshareholder;
import com.simon.annual.entity.ReportPrefshareissue;
import com.simon.annual.entity.ReportProfitdistribution;
import com.simon.annual.entity.ReportStockissue;
import com.simon.annual.repositoy.IReportDebtfinancingRepositoy;
import com.simon.annual.repositoy.IReportIndirectfinancingRepositoy;
import com.simon.annual.repositoy.IReportPrefshareholderRepositoy;
import com.simon.annual.repositoy.IReportPrefshareissueRepositoy;
import com.simon.annual.repositoy.IReportProfitdistributionRepositoy;
import com.simon.annual.repositoy.IReportStockissueRepositoy;
import com.simon.annual.service.ICompanyPublicInfo;
/**
 * 第七节:融资及分配情况
 * */
@Service
public class StockissueImpl{
	
	@Resource
	private ICompanyPublicInfo cmpanyPublicInfo;
	@Resource
	private IReportStockissueRepositoy reportStockissueRepositoy;
	@Resource
	private IReportPrefshareissueRepositoy reportPrefshareissueRepositoy;
	@Resource
	private IReportPrefshareholderRepositoy reportPrefshareholderRepositoy;
	@Resource
	private IReportIndirectfinancingRepositoy reportIndirectfinancingRepositoy;
	@Resource
	private IReportDebtfinancingRepositoy reportDebtfinancingRepositoy;
	@Resource
	private IReportProfitdistributionRepositoy reportProfitdistributionRepositoy;
	
	public ReportNeeq analysis(Element e, ReportNeeq reportNeeq, int companyPtrId) {
		//挂牌以来普通股股票发行情况 
		Element reportStockissue = cmpanyPublicInfo.findNextTag("table", e);
		Elements reportStockissueTrs = reportStockissue.select("table tr");
		if(reportStockissueTrs.size()>1){
			List<ReportStockissue> reportStockissueList = new ArrayList<ReportStockissue>();
			for (int i = 1; i < reportStockissueTrs.size(); i++) {
				Elements reportStockissueTds = reportStockissueTrs.get(i).children();
				if(reportStockissueTds.size()>1){
					ReportStockissue stockissue = new ReportStockissue();
					stockissue.setDisclosureDate(reportStockissueTds.get(0).text());
					stockissue.setIssueDate(reportStockissueTds.get(1).text());
					stockissue.setIssuePrice(reportStockissueTds.get(2).text());
					stockissue.setIssueAmount(reportStockissueTds.get(3).text());
					stockissue.setNoEmployeeIssued(reportStockissueTds.get(4).text());
					stockissue.setNoMarketMakerIssued(reportStockissueTds.get(5).text());
					stockissue.setNoExternalPersonIssued(reportStockissueTds.get(6).text());
					stockissue.setNoPrivateFundIssued(reportStockissueTds.get(7).text());
					stockissue.setNoTrustIssued(reportStockissueTds.get(8).text());
					stockissue.setIsProceedPurposeChanged(reportStockissueTds.get(9).text());
					stockissue.setRaisedValue(reportStockissueTds.get(10).text());
					stockissue.setProceedsUtilization(reportStockissueTds.get(11).text());
					reportStockissueList.add(stockissue);
				}
				reportStockissueRepositoy.save(reportStockissueList);
			}
		}
		//募集资金使用情况
		Element usedProceeds =reportStockissue;
		if(usedProceeds.nextElementSibling().tagName().equals("table")){
			usedProceeds = cmpanyPublicInfo.findNextTag("table", reportStockissue);
			reportNeeq.setUsedProceedsReview(reportStockissueTrs.last().text());
		}else{
			reportNeeq.setUsedProceedsReview(usedProceeds.text());
		}
		//二、存续至本期的优先股股票相关情况(原文本部分无此章节???)
//		Element title = reportStockissue.nextElementSibling().nextElementSibling().nextElementSibling();
		if(usedProceeds.nextElementSibling().text().indexOf("\u4F18\u5148\u80A1")>-1){
			//基本情况
			Element prefshareissue = cmpanyPublicInfo.findNextTag("table", usedProceeds.nextElementSibling());
			Elements  prefshareissueTrs =prefshareissue.select("table tr");
			if(prefshareissueTrs.first().children().size()==8){
				if(prefshareissueTrs.size()>1&&!StringUtil.isBlank(prefshareissueTrs.get(1).text().replace("-", ""))){
					List<ReportPrefshareissue> reportPrefshareissueList = new ArrayList<ReportPrefshareissue>();
					for (int i = 0; i < prefshareissueTrs.size(); i++) {
						if(i!=0){
							Elements prefshareissueTds = prefshareissueTrs.get(i).children();
							ReportPrefshareissue reportPrefshareissue = new ReportPrefshareissue();
							reportPrefshareissue.setSecurityCode(prefshareissueTds.get(0).text());
							reportPrefshareissue.setSecurityAbbr(prefshareissueTds.get(1).text());
							reportPrefshareissue.setIssuePrice(prefshareissueTds.get(2).text());
							reportPrefshareissue.setIssueShare(prefshareissueTds.get(3).text());
							reportPrefshareissue.setCouponRate(prefshareissueTds.get(5).text());
							reportPrefshareissue.setTradeStartDate(prefshareissueTds.get(6).text());
							reportPrefshareissue.setTradeEndDate(prefshareissueTds.get(7).text());
							reportPrefshareissue.setProjectScopeId(companyPtrId);
							reportPrefshareissueList.add(reportPrefshareissue);
						}
					}
					reportPrefshareissueRepositoy.save(reportPrefshareissueList);
				}
				//股东情况
				Element shareholder = cmpanyPublicInfo.findNextTag("table", prefshareissue);
				Elements shareholderTrs = shareholder.select("table tr");
				if(shareholderTrs.size()>3&&!StringUtil.isBlank(shareholderTrs.get(3).text().replace("-", ""))){
					List<ReportPrefshareholder> reportPrefshareholderList = new ArrayList<ReportPrefshareholder>();
					for (int i = 0; i < shareholderTrs.size(); i++) {
						if(i>3){
							Elements shareholderTds = shareholderTrs.get(i).children();
							ReportPrefshareholder reportPrefshareholder = new ReportPrefshareholder();
							reportPrefshareholder.setPrefShareId(shareholderTds.get(0).text());
							reportPrefshareholder.setName(shareholderTds.get(1).text());
							reportPrefshareholder.setBgnShares(shareholderTds.get(2).text());
							reportPrefshareholder.setEndShares(shareholderTds.get(3).text());
							reportPrefshareholder.setEndSharePct(shareholderTds.get(4).text());
							reportPrefshareholder.setProjectScopeId(companyPtrId);
							reportPrefshareholderList.add(reportPrefshareholder);
						}
					}
					reportPrefshareholderRepositoy.save(reportPrefshareholderList);
				}
				usedProceeds = cmpanyPublicInfo.findNextTag("table", shareholder, 4);
			}
		}
		//三、债券融资情况
		Element debtfinancing = cmpanyPublicInfo.findNextTag("table", usedProceeds);
		Elements debtfinancingTrs = debtfinancing.select("table tr");
		if(debtfinancingTrs.last().children().size()==7){
			List<ReportDebtfinancing> reportDebtfinancingList = new ArrayList<ReportDebtfinancing>();
			for (int i = 1; i < debtfinancingTrs.size()-1; i++) {
				if(i!=0){
					Elements debtfinancingTds = debtfinancingTrs.get(i).children();
					ReportDebtfinancing reportDebtfinancing = new ReportDebtfinancing();
					reportDebtfinancing.setSecurityCode(debtfinancingTds.get(0).text());
					reportDebtfinancing.setSecurityAbbr(debtfinancingTds.get(1).text());
					reportDebtfinancing.setType(debtfinancingTds.get(2).text());
					reportDebtfinancing.setProceedsValue(debtfinancingTds.get(3).text());
					reportDebtfinancing.setCouponRate(debtfinancingTds.get(4).text());
					reportDebtfinancing.setDuration(debtfinancingTds.get(5).text());
					reportDebtfinancing.setIsDefaulted(debtfinancingTds.get(6).text());
					reportDebtfinancing.setProjectScopeId(companyPtrId);
					reportDebtfinancingList.add(reportDebtfinancing);
				}
			}
			reportDebtfinancingRepositoy.save(reportDebtfinancingList);
		}
		//公开发行债券的特殊披露要求
		Element bond_public =debtfinancing.nextElementSibling();
		if(bond_public.text().indexOf("\u53D1\u884C\u503A\u5238")>-1){//发行债券
			bond_public = cmpanyPublicInfo.findNextTag("table", bond_public);
			reportNeeq.setBondPublicDisclosure(bond_public.text());
		}
		//间接融资情况
		Element indirectfinancing = cmpanyPublicInfo.findNextTag("table", bond_public);
		Elements indirectfinancingTrs = indirectfinancing.select("table tr");
		if(indirectfinancingTrs.first().children().size()==6){
			List<ReportIndirectfinancing> reportIndirectfinancingList = new ArrayList<ReportIndirectfinancing>();
			for (int i = 1; i < indirectfinancingTrs.size()-1; i++) {
				Elements indirectfinancingTds = indirectfinancingTrs.get(i).children();
				ReportIndirectfinancing reportIndirectfinancing = new ReportIndirectfinancing();
				reportIndirectfinancing.setMethod(indirectfinancingTds.get(0).text());
				reportIndirectfinancing.setFinancier(indirectfinancingTds.get(1).text());
				reportIndirectfinancing.setProceedsValue(indirectfinancingTds.get(2).text());
				reportIndirectfinancing.setDuration(indirectfinancingTds.get(3).text());
				reportIndirectfinancing.setIsDefaulted(indirectfinancingTds.get(4).text());
				reportIndirectfinancing.setProjectScopeId(companyPtrId);
				reportIndirectfinancingList.add(reportIndirectfinancing);
			}
			reportIndirectfinancingRepositoy.save(reportIndirectfinancingList);
		}
		/*违约情况
		Element financing =debtfinancing.nextElementSibling();
		if(!financing.tagName().equals("h2")){
			reportNeeq.setIndirectFinancingDefaultReview(cmpanyPublicInfo.findNextTag("table", financing).text());
		}*/
		//五、分配预案
		Element profitdistribution = cmpanyPublicInfo.findNextTag("table",indirectfinancing);
		Elements profitdistributionTrs = profitdistribution.select("table tr");
		if(profitdistributionTrs.first().children().size()==4){
			List<ReportProfitdistribution> reportProfitdistributionList = new ArrayList<ReportProfitdistribution>();
			for (int i = 1; i < profitdistributionTrs.size(); i++) {
					Elements profitdistributionTds = profitdistributionTrs.get(i).children();
					ReportProfitdistribution reportProfitdistribution = new ReportProfitdistribution();
					reportProfitdistribution.setDate(profitdistributionTds.get(0).text());
					reportProfitdistribution.setCashDividend10shares(profitdistributionTds.get(1).text());
					reportProfitdistribution.setStockDividend10shares(profitdistributionTds.get(2).text());
					reportProfitdistribution.setTransfer10shares(profitdistributionTds.get(3).text());
					reportProfitdistribution.setProjectScopeId(companyPtrId);
					reportProfitdistributionList.add(reportProfitdistribution);
			}
			reportProfitdistributionRepositoy.save(reportProfitdistributionList);
		}
		//利润分配情况
		Element pdp = cmpanyPublicInfo.findNextTag("table",indirectfinancing);
		Elements PdpTds = pdp.select("table tr > *");
		if(PdpTds.hasText()&&PdpTds.first().children().size()==4){
			reportNeeq.setPdpCashDividendShares10(PdpTds.get(5).text());
			reportNeeq.setPdpStockDividendShares10(PdpTds.get(6).text());
			reportNeeq.setPdpTransferShares10(PdpTds.get(7).text());
		}
		return reportNeeq;
	}
}
